<?php

require_once 'config/config.php';

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

$query = 'SELECT user_id, password FROM users WHERE username = "'.$username.'"';
$result = mysql_query($query);

if(mysql_num_rows($result) == 1) {
    
    $line = mysql_fetch_assoc($result);
    $user_id = $line['user_id'];
    $user_password = $line['password'];
    
    if(crypt($password, $user_password) === $user_password) {
        $query = 'SELECT * FROM telescopes t
            INNER JOIN users_telescopes ut ON (ut.telescope_ID = t.telescope_ID)
            WHERE ut.user_ID = "'.$user_id.'"';
        $result = mysql_query($query); 

        if(mysql_num_rows($result) > 0){

            $telescopen = array();
            while($line = mysql_fetch_assoc($result)){
                $telescopen[$line['telescope_ID']] = $line['name'];
            }

            $json = json_encode($telescopen);
            echo $json;

        } else {

            echo 'NOSCOPE';

        }  
    } else {
        echo 'NOSCOPE';
    }
    
    
} else {
    
    echo 'NOSCOPE';
    
}

?>
